package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"strings"
)

const todoFile = "todo.sample"

func main() {
	flag.Parse()

	// Read the todo list from disk into b.
	b, err := ioutil.ReadFile(todoFile)
	if err != nil {
		log.Fatal(err)
	}

	// Split b into items.
	items := strings.Split(string(b), "\n")

	if a := flag.Args(); len(a) > 0 {
		// Join command-line arguments together and add to items.
		s := strings.Join(a, " ")
		items = append(items, s)

		// Join the items together and store in b.
		b = []byte(strings.Join(items, "\n"))

		// Write the todo list to disk.
		err = ioutil.WriteFile(todoFile, b, 0644)
		if err != nil {
			log.Fatal(err)
		}
	}

	// List the items.
	for i, s := range items {
		fmt.Printf("%d %s\n", i, s)
	}
}
